#! /bin/bash

# Apktool Decode Script Linux
# Author: Tommy Nguyen [tommytomatoe]
# Version: 1.1.0
# Date: February 13, 2012
# Contact: tommytomatoe@gmail.com

# Setting Loop Variable
clear
APPLE=$1

# Functions
LOG=false

#enterKey
enterKey()
{	echo "### Press [enter] to continue, any other key to quit";
	read -s -n 1 key
	if [[ $key = "" ]]; then 
  		echo 
	else
  		echo
  		echo "Installation aborted";
		enter
		apk_tool
	fi
}

#enter
enter()
{	echo
	echo "Please press [enter] to continue";
	read -s -n 1 key
	if [[ $key = "" ]]; then 
  		echo
		clear
	fi
}

#enter
enterA()
{	echo
	echo "Please press [enter] when ready";
	read -s -n 1 key
	if [[ $key = "" ]]; then 
  		echo
	fi
}

#load
load_dot ()
{	echo -n "."
	sleep .1
	echo -n "."
	sleep .1
	echo -n "."
	sleep .1
	echo -n "."
	sleep .1
	echo -n "."
	sleep .1
	echo "."
}

#quit
quit()
{	echo
	echo "Thank you for choosing Android Utility"
	echo "brought to you by tommytomatoe :-)"
	echo
	echo "Please Don't Drink and Drive"
	echo
	exit
}

#select
selection ()
{	echo
	echo "You've selcted [$COM]"
	echo
}

#Decode
decode ()
{	COM="apktool d[ecode]"
	d=$HOME/android-utility/working-folder/mod-here-solo
	clear
	a=`ls -d $d/* | grep .apk`
	if [[ "$?" == "0" ]] ; then
		echo -n "Android Utility found this apk: "
		ls $d | grep .apk
		echo
		echo "Would you like to decode it? [default=Yes]"
		echo
		echo "  1. Yes"
		echo "  2. No"
		echo
		cd $d
		read -s -n 1 ans
		case $ans in 
		1) do_it_s;;
		2) enter;;
		*) do_it_s;;
		esac
	else
		clear
		echo "Android Utility did not find a valid apk file"
	fi
	enter
}

#decode Multi
decode_m ()
{	COM="apktool d[ecode]"
	d=$HOME/android-utility/working-folder/mod-here-multi
	clear
	echo
	echo "Please place apks in:"
	echo 
	echo $d 
	enterA
	a=`ls -d $d/* | grep .apk`
	if [[ "$?" == "0" ]] ; then
		echo "Android Utility found these apks: "
		echo
		cd $d
		for apkName in `ls | grep .apk`
		do
		    i=$(($i+1))
		    apkArray[$i]=$apkName
		    echo "  $i. $apkName" >> temp.list
		done
		cat temp.list
		rm -f temp.list
		echo
		echo -n "Please enter number of Apk to decompile (all=0, quit=q): "
		read ans
		echo
		if [ "$ans" == "0" ]; then
			do_it_m
		fi
		if [ "$ans" == "" ]; then
			echo "Please enter a valid option"
			echo
			exit 0
		fi	
		if [ "$ans" == "q" ]; then
			exit 0
		fi
		if [ "`echo $ans | sed 's/[0-9]*//'`" == "" ] || [ "ans"=="1" ]; then
			apkDecode=${apkArray[$ans]}
			if [ "$apkDecode" == "" ]; then
				exit 0
			fi
		else
			exit 0
		fi	
		apktool d -f $apkDecode $HOME/android-utility/working-folder/mod-here-multi/Projects/${apkDecode/.apk}	
		echo
		cd $d
		echo "Extracting original META_iNF (signature) from apk"
		if [[ -s original_META-INF ]] ; then
			rm -r original_META-INF
			mkdir original_META-INF
		else
			mkdir original_META-INF
		fi
		for c in $apkDecode ; do 
		echo "Extracting Original META-INF from $c" 
		unzip -o $c META-INF/* -d original_META-INF/"$c"-META-INF &>/dev/null
		load_dot
		done
		echo
		echo "$apkDecode is now in mod-here-multi/Projects"
		if "$LOG" = "true" ; then
			echo
			echo "Please check decode_log for apks with errors"
		fi
	else
		echo
		clear
		echo "Android Utility did not find a valid apk file"
	fi
	enter
}

#do it
do_it_s ()
{	for b in *.apk
	do
	echo "Decoding $b to ../mod-here-solo/${b/.apk}:"
	echo 
	apktool d -f $b
	if [[ "$?" -ne "0" ]] ; then
		echo "On `date +%b%e_%H%M%S`, $b was not Decoded Properly" >> $HOME/android-utility/log/decode_log
		LOG=true
	fi
	echo
	done
	echo "Would you like to extract original META-INF? [default=Yes]"
	echo
	echo "  1. Yes"
	echo "  2. No"
	echo
	read ans
	case $ans in
		1) extract_mi;;
		2) echo;;
		*) extract_mi;;
	esac
	if "$LOG" = "true" ; then
		echo
		echo "Please check decode_log for apks with errors"
	fi
}

extract_mi ()
{	if [[ -s original_META-INF ]] ; then
		rm -r original_META-INF
		mkdir original_META-INF
	else
		mkdir original_META-INF
	fi
	for c in *.apk ; do 
	echo "Extracting Original META-INF from $c" 
	unzip -o $c META-INF/* -d original_META-INF &>/dev/null
	load_dot
	done
}

extract_miM ()
{	if [[ -s original_META-INF ]] ; then
		rm -r original_META-INF
		mkdir original_META-INF
	else
		mkdir original_META-INF
	fi
	for c in *.apk ; do 
	echo "Extracting Original META-INF from $c" 
	unzip -o $c META-INF/* -d original_META-INF/"$c"-META-INF &>/dev/null
	load_dot
	done
}

do_it_m ()
{	for b in *.apk
	do
	echo "Decoding $b to ../mod-here-multi/Projects/${b/.apk}:"
	echo 
	apktool d -f $b $HOME/android-utility/working-folder/mod-here-multi/Projects/${b/.apk}
	if [[ "$?" -ne "0" ]] ; then
		echo "On `date +%b\ %e\ %H:%M:%S`, $b was not Decoded Properly" >> $HOME/android-utility/log/decode_log
	LOG=true
	fi	
	echo
	done
	echo
	cd $d
	echo "Would you like to extract original META-INF? [default=Yes]"
	echo
	echo "  1. Yes"
	echo "  2. No"
	echo
	read ans
	case $ans in
		1) extract_miM;;
		2) echo;;
		*) extract_miM;;
	esac
	enter
}



if [[ "$1" == "" ]] ; then
	decode
elif [[ "$1" == "-m" ]] ; then
	decode_m
fi

